<?php
/*
 This file provides the functions to deal with Vacancy Issues
 */
include_once 'dbconfig.php';

class Transaction {

	function Transaction() {
		/*
		 This is the constructor of Vacancy class
		 Connect to the database
		 */
		mysql_connect(DBSERVER, DBUSER, DBPASS);  // from dbconfig.php
		$selectdb = mysql_select_db(DBNAME);
	}	

	function mail_to_landlord($vacancy_id,$title,$landlord_id,$email) {
		if (isset($_SESSION['username']))
		{
		$to = $email;
		$subject = "Transaction request from Student Housing Portal";
		$text = $_SESSION['username']." is requesting for a transaction on your vacancy ".$title."View all transactions for you here!  http://sit.rp.edu.sg/73943/fypcoding/view_transaction.php?landlord_id=".$landlord_id.">";
		$from = "74023@myrp.edu.sg";
		$headers = "From: " .$from;
			if(mail ($to, $subject, $text, $headers))
			{
			echo "Mail Sent. Redirect to previous page in 10 seconds.";
         			if (isset($HTTP_REFERER)) { 
				echo "<a href='$HTTP_REFERER'>BACK</a>"; 
				} else { 
				echo "<a href=view_vacancy_detail.php?vacancy_id=".$vacancy_id.">BACK</a>"; 
				} 
			}
		}
	}
	
	function request_for_transaction($vacancy_id, $landlord_id, $tenant_id) {
		$sql = "INSERT INTO transaction (vacancy_id, tenant_id, landlord_id, transaction_status) VALUES ('".$vacancy_id."','".$tenant_id."','".$landlord_id."','0')";
		mysql_query($sql)or die (mysql_error());
	}
	
	function list_transaction($type,$type_id) {
		if($type==2)
		{
			//$type_id = $_SESSION['type_id'];
			$sql = "SELECT * FROM transaction WHERE landlord_id = '".$type_id."'";
			$result = mysql_query($sql)or die (mysql_error());
			$return_array = array();
			while ($row = mysql_fetch_assoc($result)) {
				$return_array[] = $row;
			}
		}
		else if($type==3)
		{

			//$tenant_id = $_SESSION['type_id'];
			$sql = "SELECT * FROM transaction WHERE tenant_id = '".$type_id."'";
			$result = mysql_query($sql)or die (mysql_error());
			$return_array = array();
			while ($row = mysql_fetch_assoc($result)) {
				$return_array[] = $row;
			}
		}
                else if($type==1)
		{

			//$tenant_id = $_SESSION['type_id'];
			$sql = "SELECT * FROM transaction";
			$result = mysql_query($sql)or die (mysql_error());
			$return_array = array();
			while ($row = mysql_fetch_assoc($result)) {
				$return_array[] = $row;
			}
		}

		return $return_array;
	}
	
	function get_transaction($transaction_id) {
		$sql = "SELECT * FROM transaction WHERE transaction_id = '".$transaction_id."'";
		$result = mysql_query($sql)or die (mysql_error());
		$row = mysql_fetch_assoc($result);
		return $row;
	}
	
	function update_transaction($transaction_id,$transaction_status) {
		$update_result = "true";
		if($transaction_status == 0)
		{
		$sql = "UPDATE transaction, vacancy SET transaction.transaction_starttime=NOW(), transaction.transaction_status='1', vacancy.vacancy_available='0' WHERE transaction.transaction_id='".$transaction_id."' AND transaction.vacancy_id=vacancy.vacancy_id";
		$result = mysql_query($sql)or die (mysql_error());
		}
		else if($transaction_status == 1)
		{
		$sql = "UPDATE transaction SET transaction_endtime=NOW(), transaction_status='2' WHERE transaction_id='".$transaction_id."'";
		$result = mysql_query($sql)or die (mysql_error());
		}
		else
		{
		$update_result = "false";
		}
		return $update_result;
	}
	
	function check_transaction($tenant_id) {
		$check = "false";
		$sql = "SELECT * FROM transaction WHERE tenant_id=".$tenant_id."";
		$result = mysql_query($sql)or die (mysql_error());
		$countrows= mysql_num_rows($result);
		if($countrows==1)
		{
		$check = "true";
		}
		return $check;
	}
	
	function check_empty_rating($user_type, $type_id) {
		if($user_type == 2)
		{
		$sql = "SELECT transaction_id FROM transaction WHERE landlord_id='".$type_id."' AND landlord_rate='0' AND transaction_status='2'";
		}
		else if($user_type == 3)
		{
		$sql = "SELECT transaction_id FROM transaction WHERE tenant_id='".$type_id."' AND tenant_rate='0' AND transaction_status='2'";
		}
		$result = mysql_query($sql)or die (mysql_error());
		$countrows= mysql_num_rows($result);
		return $countrows;
	}
	
	function mail_to_tenant($tenant_id, $email) {
		if (isset($_SESSION['username']))
		{
		$to = $email;
		$subject = "Transaction request from Student Housing Portal";
		$text = $_SESSION['username']." has just updated your transaction. View your transaction here!  http://sit.rp.edu.sg/73943/fypcoding/view_transaction.php?tenant_id=".$tenant_id.">";
		$from = "74023@myrp.edu.sg";
		$headers = "From: $from";
		mail ($email, $subject, $text, $headers);
		}
	}
	
	function do_rate_transaction($transaction_id, $user_type, $rate_for_user_id, $rate_score) {
		$rate_result = "false";
		if($user_type == 2)
		{
			$sql="UPDATE transaction SET landlord_rate = 1 WHERE transaction_id = ".$transaction_id."";
			$result = mysql_query($sql)or die (mysql_error());
			$sql="UPDATE member SET member_score = ".$rate_score." WHERE member_id = ".$rate_for_user_id."";
			$result = mysql_query($sql)or die (mysql_error());
			$rate_result = "true";
		
		}
		else if($user_type == 3)
		{
			$sql="UPDATE transaction SET tenant_rate = 1 WHERE transaction_id = ".$transaction_id."";
			$result = mysql_query($sql)or die (mysql_error());
			$sql="UPDATE member SET member_score = ".$rate_score." WHERE member_id = ".$rate_for_user_id."";
			$result = mysql_query($sql)or die (mysql_error());
			$rate_result = "true";
		}
		return $rate_result;
	}
}